package com.cloudcc.mobile.view.base;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import butterknife.ButterKnife;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.mobile.R;


/**
 * fragment 基类
 * 
 * @author wangzhiqiang
 * @date 2015-4-27 下午2:56:48
 * 
 */
public abstract class CFragment extends Fragment implements OnCancelListener {

	protected boolean isFirstInit = true;
	protected View convertView;// 缓存 view
	protected Context mContext; 

	@Override
	public void onAttach(Activity activity) {
		this.mContext =activity;
		super.onAttach(activity);
	}
	/**
	 * 获得 fragment 管理器
	 * 
	 * @return
	 */
	public FragmentManager getFragmentMag() {
		FragmentManager supportFragmentManager = getActivity()
				.getSupportFragmentManager();
		return supportFragmentManager;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (convertView == null) {
			convertView = inflater.inflate(getLayoutId(), container, false);
		}
		ViewGroup parent = (ViewGroup) convertView.getParent();
		if (parent != null) {
			parent.removeView(convertView);
		}
		ButterKnife.bind(this, convertView);
		return this.convertView;
	}

	/**
	 * 移动fragment
	 * 
	 * @param mFragment
	 *            目标 fragment
	 * @param isStack
	 *            是否加载到返回栈
	 * @param bundle
	 *            携带的数据
	 */
	protected void moveFragment(Fragment mFragment, boolean isStack,
			Bundle bundle) {
		//getFragmentManager().
	}

	public void startFragment(FragmentActivity activity,int containerId,Fragment target,boolean isStack, Bundle bundle) {
		if (bundle != null) {
			target.setArguments(bundle);
		}
		FragmentTransaction transaction = activity.getSupportFragmentManager()
				.beginTransaction();
		transaction
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
				.setCustomAnimations(R.anim.slide_in_from_left,R.anim.slide_out_to_right)
				.replace(containerId,target);
		if (isStack) {
			transaction.addToBackStack(target.getClass().getName());
		}
		transaction.commit();
	}

	/**
	 * 取消dialog的时候
	 * */
	@Override
	public void onCancel(DialogInterface dialog) {
		onCancelWaitDialog(dialog);
	}

	/**
	 * 正在加载取消的时候回调
	 * 
	 * @param dialog
	 */
	public void onCancelWaitDialog(DialogInterface dialog) {
	}

	protected View findViewById(int id) {
		return getView().findViewById(id);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		if (isFirstInit) {
			if (hasEventLife()) {
				EventEngine.register(this);
			}
			init();
			isFirstInit =false;
		}
	}

	/**
	 * 初始化
	 */
	public abstract void init();

	public abstract int getLayoutId();
	
	/**是否 注册到总线
	 * @return
	 */
	protected boolean hasEventLife() {
		return false;
	}

	/**
	 * 设置事件监听
	 */
	public void setListener() {
	}
	
	@Override
	public void onDestroy() {
		if (hasEventLife()) {
			EventEngine.uregister(this);
		}
		super.onDestroy();
		
	}
	
    /**
     * show toast
     *
     * @param msg
     */
    protected void showToast(String msg) {
        if (null != msg && !TextUtils.isEmpty(msg)) {
        	Toast.makeText(mContext, msg, 0).show();
        }
    }
	
	
}
